package com.xiao.student.controller;

import com.alibaba.fastjson.JSONObject;
import com.xiao.student.bean.Fangtan;
import com.xiao.student.bean.Renyuan;
import com.xiao.student.service.FangtanService;
import com.xiao.student.service.RenyuanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by 衰草寒烟 on 2017/2/14.
 */
@Controller("fangtan")
@RequestMapping("/ft")
public class FangtanController {
    @Autowired
    private FangtanService fangtanservice;
    @Autowired
    private RenyuanService ryservice;

    /**
     * 访谈界面
     */
    @RequestMapping("/addft")
    public ModelAndView addft(Integer id){
        ModelAndView mv=new ModelAndView();
        Renyuan rs = ryservice.getr(id);
        mv.addObject("renyuan",rs);
        mv.setViewName("addft");
        return mv;
    }
    /**
     * 增加访谈
     * @param
     * @return
     */
    @RequestMapping("/saveft")
    @ResponseBody
    public String ft(HttpServletRequest request){
        String  msg="false";
        Fangtan ft=new Fangtan();
        //Renyuan ry=ryservice.getbysjh(request.getParameter("bftrsjh"));
//        ft.setId(Integer.valueOf(request.getParameter("id")));
        ft.setFtr(Integer.valueOf(request.getParameter("ftr")));
        ft.setBftr(Integer.valueOf(request.getParameter("id")));
        String date=request.getParameter("date");
        SimpleDateFormat sf=new SimpleDateFormat("YYYY-MM-dd hh:mm:ss");
        try {
            ft.setDate(sf.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        ft.setNr(request.getParameter("nr"));
        ft.setJg(request.getParameter("jg"));
        boolean rs = fangtanservice.saveft(ft);
        if (rs){
            msg="success";
        }
        JSONObject json=new JSONObject();
        json.put("msg",msg);
        return json.toJSONString();
    }

    /**
     * 导向访谈页面
     */
    @RequestMapping("/getft")
    public String getft(HttpServletRequest request, @RequestParam(value = "id")int bftr){
        List<Fangtan> rs = fangtanservice.getft(bftr);
        Renyuan ry=ryservice.getr(bftr);
        request.setAttribute("ftlist",rs);
        request.setAttribute("user",ry);
        return "fangtan";
    }
}
